// 引入express模块
const express = require('express')
// 创建web服务器实例
const serve = express()
// 引入cors模块
const cors = require('cors')
const md5 = require('md5')


// 引入接口
const user = require('./router/user.js')
const shouye = require('./router/shouye.js')
const aill = require('./router/aill.js')
const major = require('./router/major.js')
const literary = require('./router/literary.js')
const fantasy = require('./router/fantasy.js')
const partyhistory = require('./router/partyhistory.js')
const science_fiction = require('./router/science_fiction.js')
serve.use(
    cors({
    // 允许所有域名访问
    origin: [
        'http://localhost:8081', 'http://127.0.0.1:3001'
    ]
}))
serve.listen(3001, () => {
    console.log('服务器启动了')
})

// 将post传参转为对象
serve.use(
    express.urlencoded({
        extended: true
    })
)

serve.use(express.static("./public"))

serve.use('/v1/user', user)
serve.use('/v1/shouye', shouye)
serve.use('/v1/aill', aill)
serve.use('/v1/major', major)
serve.use('/v1/literary', literary)
serve.use('/v1/fantasy', fantasy)
serve.use('/v1/partyhistory', partyhistory)
serve.use('/v1/science_fiction', science_fiction)
// 错误处理中间件
serve.use((err, req, res, next) => {
    if (err) {
        console.log(err)
    }
    res.send({ code: 502, msg: '服务器端错误' })
})